<!DOCTYPE html>
<html>
<head>
    <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css"/>
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <script src="/js/common.js"></script>
    <script src="/js/myJs.js"></script>
    <script>
        var praise;
        var strategyId;
        var praise2;
        var user = JSON.parse(sessionStorage.getItem("user"));
        var strategyId;
        var currentPage = 1; //当前页
        var pages; //总页数
        var comments;//点评
        var reviewId;//点评id
        $(function () {
            strategyId = getParams().strategyId;
            $("#commentBtn").click(function () {
                window.location.href="/mine/addComment.html?strategyId="+strategyId;
            })

            //点赞功能的实现
            if (user){
                $.get("/praises",{userId: user.id, typeId: strategyId, type: 1},function (data) {
                    if (data.success){
                        praise = data.data;
                        if (praise.state==1){
                            $("#likeBtn").attr("class", "fa fa-thumbs-up");
                        }
                    }
                })
            }
            //发送请求获取当前的攻略信息
            $.get("/strategies/"+strategyId, function (data) {
                $("#title").html(data.title);
                //获得点赞数
                $("#praiseNum").html(data.praiseNum);
                sessionStorage.setItem("praiseNum",data.praiseNum);
            });



            //发送ajax请求获取当前攻略的分类
            $.get("/strategies/"+strategyId+"/catalogs", function (data) {
                var num = 1;
                $("#pills-catalog .catalogs").renderValues({list: data}, {
                    showTitle: function (ele, value) {
                        // 01/概况
                        var content = (num++)+"/"+value;
                        if (num < 10) {
                            content = 0 + content;
                        }
                        ele.innerHTML = content;
                    },
                    handle: function (ele, value) {
                        ele.href = "/strategyDetails.html?id="+value;
                    }
                });
            });





            //大家都在看
            $.get("/strategies/" + strategyId + "/travels" , function (data) {
                $("#pills-catalog .hot").renderValues(data , {
                    handle: function (ele , value) {
                        ele.href = "/travelContent.html?id="+value;
                    }
                })
            });

            //点了点赞小拇指的时候
            $("#likeBtn").click(function () {
                var num = JSON.parse(sessionStorage.getItem("praiseNum"));
                if (!user) {
                    $(document).dialog({
                        type: "confirm",
                        style: "IOS",
                        titleShow: false,
                        content: "请登录",
                        buttons: [{
                            name: "跳转到登录界面",
                            callback: function () {
                                location.href = "/login.html";
                            }
                        }]
                    });
                    return;
                }
                //如果是第一次点赞,那么我们就增加一条记录
                if (!praise) {
                    $.post("/praises", {userId: user.id, typeId: strategyId, type: 1, state: 1}, function (data) {
                        praise = data.data;
                        $("#likeBtn").attr("class", "fa fa-thumbs-up")
                        num++;
                        $("#praiseNum").html(num);
                        sessionStorage.setItem("praiseNum",num);
                        return;
                    })
                }
                //如果是切换,那么我们换下状态
                //取消赞
                if (praise.state == 1) {
                    praise.state = 0;
                    $.post("/praises", praise, function (data) {
                        praise = data.data;
                        $("#likeBtn").attr("class", "fa fa-thumbs-o-up")
                        num--;
                        $("#praiseNum").html(num);
                        sessionStorage.setItem("praiseNum",num);
                    })
                } else {
                    //点赞
                    praise.state = 1;
                    $.post("/praises", praise, function (data) {
                        praise = data.data;
                        $("#likeBtn").attr("class", "fa fa-thumbs-up")
                        num++;
                        $("#praiseNum").html(num);
                        sessionStorage.setItem("praiseNum",num);
                    })
                }

            });

            var praiseNum;
            //查所有的点评
            $.get("/comments",{currentPage:1,pageSize:2,strategyId:strategyId},function (data) {
                comments=data.list;
                pages = data.pages;
                $("#comment").renderValues(data,{
                    handleUserProfiles: function (ele, value) {
                        ele.href = "/userProfiles.html?id="+value;
                    },
                    handleStrategyComment: function (ele, value) {
                        ele.href = "/strategyComment.html?id=" + value;
                    },
                    handleLike: function (ele, value) {
                        $(ele).attr("data-rid", value);
                        //  给每个span设置标志
                        $(ele).find("span").attr("data-pid", value);
                        $.get("/praises/" + value, {type: 3}, function (data) {
                            // 通过标志给每个span设置属性
                            praiseNum=data;
                            $("span[data-pid=" + value + "]").html(praiseNum);

                        });
                        //判断我们之前是否有点赞
                        if (user) {
                            // 获取当前用户的状态值做回显
                            $.get("/praises",
                                {userId: user.id, typeId: value, type: 3},
                                function (data) {
                                    var p = data.data;

                                    $("[data-i=" + value + "]").removeClass();
                                    if (p) {
                                        if (p.state == 1) {
                                            $("[data-rid=" + value + "]").addClass("fa fa-thumbs-up");
                                        } else {
                                            $("[data-rid=" + value + "]").addClass("fa fa-thumbs-o-up");

                                        }
                                    }
                                }
                            );
                        }

                    }
                });
                $.each(data.list,function (index,item) {
                    //亮星星的
                    for (var i = 1; i <= item.star; i++) {
                        $("input[value=" + item.id + "]~i[data-num=" + i + "]").attr("class", "fa fa-star fa-lg")
                    }
                })
            })
            //设置窗口的滚到事件
            $(window).scroll(function () {
                if (isEnd()) {
                    if (currentPage < pages) {
                        currentPage++;
                        var args = {currentPage:currentPage,pageSize:2,strategyId:strategyId};
                        $.get("/comments", args, function (data) {
                            //数据渲染
                            $.merge(comments, data.list);
                            $("#comment").renderValues({list:comments},{
                                handleUserProfiles: function (ele, value) {
                                    ele.href = "/userProfiles.html?id="+value;
                                },
                                handleStrategyComment: function (ele, value) {
                                    ele.href = "/strategyComment.html?id=" + value;
                                },
                                handleLike: function (ele, value) {
                                    $(ele).attr("data-rid", value);
                                    //  给每个span设置标志
                                    $(ele).find("span").attr("data-pid", value);
                                    $.get("/praises/" + value, {type: 3}, function (data) {
                                        // 通过标志给每个span设置属性
                                        praiseNum=data;
                                        $("span[data-pid=" + value + "]").html(praiseNum);

                                    });
                                    //判断我们之前是否有点赞
                                    if (user) {
                                        // 获取当前用户的状态值做回显
                                        $.get("/praises",
                                            {userId: user.id, typeId: value, type: 3},
                                            function (data) {
                                                var p = data.data;
                                                $("[data-i=" + value + "]").removeClass();
                                                if (p) {
                                                    if (p.state == 1) {
                                                        $("[data-rid=" + value + "]").addClass("fa fa-thumbs-up");
                                                    } else {
                                                        $("[data-rid=" + value + "]").addClass("fa fa-thumbs-o-up");

                                                    }
                                                }
                                            }
                                        );
                                    }

                                }
                            });
                            $.each(comments,function (index,item) {
                                //亮星星的
                                for (var i = 1; i <= item.star; i++) {
                                    $("input[value=" + item.id + "]~i[data-num=" + i + "]").attr("class", "fa fa-star fa-lg")
                                }
                            })
                        });
                    } else {
                        $(document).dialog({
                            type : "notice",
                            infoText: "已经到底部了",
                            autoClose: 1500,
                            position: "center"
                        });
                    }
                }
            });



            $("#comment").on("click","[name=pp]",function () {
                if (!user) {
                    $(document).dialog({
                        type: "confirm",
                        style: "IOS",
                        titleShow: false,
                        content: "请登录",
                        buttons: [{
                            name: "跳转到登录界面",
                            callback: function () {
                                location.href = "/login.html";
                            }
                        }]
                    });
                    return;
                }
                var rid = $(this).data("rid")
                console.log(rid);
                //先去查询是否已经有了这个对应的赞
                $.get("/praises",{userId: user.id, typeId: rid, type: 3},function (data) {
                    if (data.success){
                        //只是修改操作
                        if (data.data.state==1){
                            $.ajax({
                                url:"/praises",
                                type:"PUT",
                                data:{userId: user.id, typeId: rid, type: 3, state: 0},
                                success:function () {
                                    $("[data-rid=" + rid + "]").attr("class","fa fa-thumbs-o-up");
                                    var num = parseInt($("span[data-pid=" + rid + "]").html())
                                    $("span[data-pid=" + rid + "]").html(num-1);
                                }
                            })
                        }else {
                            $.ajax({
                                url:"/praises",
                                type:"PUT",
                                data:{userId: user.id, typeId: rid, type: 3, state: 1},
                                success:function () {
                                    $("[data-rid=" + rid + "]").attr("class","fa fa-thumbs-up");
                                    var num = parseInt($("span[data-pid=" + rid + "]").html())
                                    $("span[data-pid=" + rid + "]").html(num+1);
                                }
                            })
                        }
                    } else {
                        //新增操作
                        $.post("/praises",{userId: user.id, typeId: rid, type: 3, state: 1},function (data) {
                            $("[data-rid=" + rid + "]").attr("class","fa fa-thumbs-up");
                            var num = parseInt($("span[data-pid=" + rid + "]").html())
                            $("span[data-pid=" + rid + "]").html(num+1);
                        })
                    }
                })
            })

            //显示标签
            $.get("/strategies/"+strategyId+"/tags",function (data) {
                $.each(data,function (index, item) {
                    $("#tags").append("<span>"+item.content+"</span>");
                })
            });





        });
    </script>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="搜索旅行攻略">
            </div>
        </div>
    </div>
</div>

<div class="container strategies">
    <h5 class="title" id="title"></h5>
    <small><span>0</span>人阅读过此攻略</small>
    <div class="focus">
        <i class="fa fa-heart"><span> 0</span> 收藏</i>
        <i class="fa fa-thumbs-o-up" id="likeBtn"><span id="praiseNum"></span> 点赞</i>
        <button class="btn">想去</button>
        <button class="btn">去过</button>
    </div>
</div>
<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab">
    <li class="nav-item ">
        <a class="nav-link active" id="pills-catalog-tab" data-toggle="pill" href="#pills-catalog">
            <span>攻略目录</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link " id="pills-comment-tab" data-toggle="pill" href="#pills-comment">
            <span>骡友点评</span>
        </a>
    </li>
</ul>
<div class="tab-content " id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-catalog">
        <div class="container hot">
            <h6>大家都在看</h6>
            <div render-loop="list">
                <a render-key="list.travel.id" render-fun="handle">
                    <button class="btn" render-html="list.travel.title"></button>
                </a>
            </div>
        </div>
        <div class="catalogs">
            <div render-loop="list">
                <div class="container detail">
                    <h6 render-key="list.name" render-fun="showTitle"></h6>
                    <div render-loop="list.details">
                        <a href="strategyDetails.html" render-key="list.details.id" render-fun="handle">
                            <button class="btn" render-html="list.details.title"></button>
                        </a>
                    </div>
                </div>

            </div>
        </div>

    </div>
    <div class="tab-pane fade " id="pills-comment">

        <div class="container tag">
            <h6>骡友标签</h6>
            <div id="tags">

            </div>
        </div>
        <hr>


        <div id="comment">
            <div  render-loop="list">
                <div class="container row comment" >
                    <div class="col-2 comment-head">
                        <a href="userProfiles.html" render-key="list.userId" render-fun="handleUserProfiles">
                            <img class="rounded-circle"
                                 render-src="list.uheadImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right">
                        <p class="authorName"><span render-html="list.unickName"></span></p>
                        <span class="comment-star" render-data-star="list.id">
                            <input type="hidden" name="star" class="star" render-value="list.id">
                            <i class="fa fa-star-o fa-lg" style="color: #DFE823" data-num="1"></i>
                            <i class="fa fa-star-o fa-lg" style="color: #DFE823" data-num="2"></i>
			                <i class="fa fa-star-o fa-lg" style="color: #DFE823" data-num="3"></i>
			                <i class="fa fa-star-o fa-lg" style="color: #DFE823" data-num="4"></i>
                            <i class="fa fa-star-o fa-lg" style="color: #DFE823" data-num="5"></i></span>

                        <span class="comment-date" render-html="list.releaseTime"></span>
                        <div class="comment-content">
                            <a href="strategyComment.html" render-key="list.id" render-fun="handleStrategyComment">
                                <p render-html="list.content"></p>
                            </a>
                        </div>

                        <ul class="comment-img" render-loop="list.singlePic">
                            <li>
                                <img render-src="list.singlePic.pic">
                            </li>
                        </ul>

                        <div class="d-flex icon">
                            <i class="fa  fa-commenting-o " style="margin-right: 10px"> 评论 </i>
                            <i class="fa fa-thumbs-o-up" name="pp" render-key="list.id" render-fun="handleLike"><span>0</span>
                            </i>
                        </div>

                    </div>
                </div>

            </div>
        </div>
    </div>
</div>
</div>

<div class="operation">
    <button class="btn">加入旅行单</button>
    <button class="btn" id="commentBtn">点评</button>
</div>


</body>
</html>